<!-- 占比饼状图 -->
<template>
  <div ref="chartRef" :style="{ width, height }"></div>
</template>
<script lang="ts" setup>
  import { Ref, ref, watch } from 'vue';
  import echarts from '/@/utils/lib/echarts';
  import { useECharts } from '/@/hooks/web/useECharts';

  const props = defineProps({
    loading: Boolean,
    width: {
      type: String as PropType<string>,
      default: '100%',
    },
    height: {
      type: String as PropType<string>,
      default: '300px',
    },

    seriesVal: {
      type: Object,
      default: () => {},
    },
  });

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);

  watch(
    () => props.loading,
    () => {
      if (props.loading) {
        return;
      }
      const {Xdata , seriesValue} = props.seriesVal
      // console.log(seriesValue)
      if (seriesValue && seriesValue.length === 0) {
        setOptions({
          title: {
            textStyle: {
              color: 'grey',
              fontSize: 20,
            },
            text: '暂无数据',
            left: 'center',
            top: 'center',
          },
        });
      } else {
        setOptions({
          grid: {
            top: 10,
            left: 10,
            right: 20,
            bottom: 0,
            containLabel: true,
          },
          tooltip: {
            trigger: 'item',
            formatter: "{b} : {d}%"
          },
          color: ['#FFCC00', '#33CCFF', '#003399', '#FF6600', '#999999'],
          legend: {
              orient: 'vertical',
              left: 10,
              //right: 10,
              top: 20,
              //bottom: 20,
              data: Xdata
          },
          series: [
            {
                name: '',
                type: 'pie',
                radius : '55%',
                center: ['50%', '60%'],
                itemStyle: {},
                label: {
                  normal: {
                    formatter: '{b} : {d}% ',
                    rich: {
                      per: {
                            color: '#eee',
                            backgroundColor: '#334455',
                            padding: [2, 4],
                            borderRadius: 13,
                            // center: ['50%', '50%']
                            //  position: 'inside',
                        }
                    }
                  }
                },
                data: seriesValue,
            },
          ],
          ...props.seriesVal,
        });
      }
    },
    { immediate: true },
  );
</script>
